package com.xingongchang.baolebei.adapter;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.res.Resources;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.nostra13.universalimageloader.core.ImageLoader;
import com.xingongchang.baolebei.BaolebeiApp;
import com.xingongchang.baolebei.R;
import com.xingongchang.baolebei.activity.LogisticsActivity;
import com.xingongchang.baolebei.activity.ProductDetailActivity;
import com.xingongchang.baolebei.activity.ShippingStatusActivity;
import com.xingongchang.baolebei.model.OrderModel;
import com.xingongchang.baolebei.protocol.GOODORDER;
import com.xingongchang.baolebei.protocol.ORDER_GOODS_LIST;

public class HistoryAdapter extends BaseAdapter {

	private Context context;
	public List<GOODORDER> list;
	public int flag;
	int admin=0;
	private LayoutInflater inflater;
	
	public Handler parentHandler;
	public boolean iscomment=false;
	
	private SharedPreferences shared;
	private SharedPreferences.Editor editor;
	public Button button;
	OrderModel model;
	String topicId,shopping_name;
    protected ImageLoader imageLoader = ImageLoader.getInstance();
	
	public HistoryAdapter(Context context, List<GOODORDER> list, int flag) {
		this.context = context;
		this.list = list;
		this.flag = flag;
		inflater = LayoutInflater.from(context);
		model = new OrderModel(context);
	}
	
	
	@Override
	public int getCount() {
		 
		return list.size();
	}

	@Override
	public Object getItem(int position) {
		 
		return list.get(position);
	}

	@Override
	public long getItemId(int position) {
		 
		return position;
	}
	

	@Override
	public int getItemViewType(int position) {
		 
		return 1;
	}


	@Override
	public int getViewTypeCount() {
		 
		return 1;
	}


	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		 
		final ViewHolder holder;
        final Resources resource = (Resources) context.getResources();
	/*	if(convertView == null) {*/
			holder = new ViewHolder();
			convertView = inflater.inflate(R.layout.history_cell, null);
			holder.sno = (TextView) convertView.findViewById(R.id.trade_item_sno);
			holder.time = (TextView) convertView.findViewById(R.id.trade_item_time);
			holder.body = (LinearLayout) convertView.findViewById(R.id.trade_item_body);
			holder.fee = (TextView) convertView.findViewById(R.id.trade_item_fee);
//			holder.red_paper = (TextView) convertView.findViewById(R.id.trade_item_redPaper);
//			holder.score = (TextView) convertView.findViewById(R.id.trade_item_score);
			holder.total = (TextView) convertView.findViewById(R.id.trade_item_total);
    		holder.check = (Button) convertView.findViewById(R.id.trade_item_check);
			holder.ok = (Button) convertView.findViewById(R.id.trade_item_ok);
			final ArrayList<ORDER_GOODS_LIST> goods_list = list.get(position).goods_list;
			final GOODORDER order = list.get(position);
			for(int i=0;i<goods_list.size();i++) {
				admin=i;
				View view = LayoutInflater.from(context).inflate(R.layout.trade_body, null);
				ImageView image = (ImageView) view.findViewById(R.id.trade_body_image);
				TextView text = (TextView) view.findViewById(R.id.trade_body_text);
				TextView total = (TextView) view.findViewById(R.id.trade_body_total);
				TextView num = (TextView) view.findViewById(R.id.trade_body_num);
				button=(Button) view.findViewById(R.id.item_check);
				LinearLayout historyGoods = (LinearLayout) view.findViewById(R.id.history_goods);
				holder.body.addView(view);
				
				shared = context.getSharedPreferences("userInfo", 0); 
				editor = shared.edit();
				String imageType = shared.getString("imageType", "mind");
				
				if(imageType.equals("high")) {
                    imageLoader.displayImage(goods_list.get(i).img.thumb,image, BaolebeiApp.options);
				} else if(imageType.equals("low")) {
                    imageLoader.displayImage(goods_list.get(i).img.small,image, BaolebeiApp.options);
				} else {
					String netType = shared.getString("netType", "wifi");
					if(netType.equals("wifi")) {
                        imageLoader.displayImage(goods_list.get(i).img.thumb,image, BaolebeiApp.options);
					} else {
                        imageLoader.displayImage(goods_list.get(i).img.small,image, BaolebeiApp.options);
					}
				}
				text.setText(goods_list.get(i).name);
				total.setText(goods_list.get(i).subtotal);
				num.setText("X "+goods_list.get(i).goods_number);
				
			/*	if("".equals(order.invoice_no) ||  "".equals(order.shipping_name)){
					holder.check.setVisibility(View.GONE);
				}else{
					holder.check.setVisibility(View.VISIBLE);
					holder.check.setOnClickListener(new OnClickListener() {
						@Override
						public void onClick(View arg0) {
							Intent intent=new Intent(context,LogisticsActivity.class);
							intent.putExtra("shipping_name", order.shipping_name);
							intent.putExtra("invoice_no", order.invoice_no);
							context.startActivity(intent);
						}
					});
				}*/
				if(flag==3){
					if("".equals(order.invoice_no) ||  "".equals(order.shipping_name)){
						holder.check.setVisibility(View.GONE);
					}else{
						holder.check.setVisibility(View.VISIBLE);
						holder.check.setOnClickListener(new OnClickListener() {
							@Override
							public void onClick(View arg0) {
								Intent intent=new Intent(context,LogisticsActivity.class);
								intent.putExtra("shipping_name", order.shipping_name);
								intent.putExtra("invoice_no", order.invoice_no);
								context.startActivity(intent);
							}
						});
					}
				}
				final int index = i;
				historyGoods.setOnClickListener(new OnClickListener() {
					@Override
					public void onClick(View arg0) {
						Intent intent=new Intent(context,
								ProductDetailActivity.class);
					    intent.putExtra("good_id",goods_list.get(index).goods_id);
						context.startActivity(intent);
						Log.i("tag", ""+goods_list.get(index).goods_id);
					}
				});
		
				if(goods_list.get(admin).commented==0){
					if(flag==1 || flag==2 || flag==3){
						button.setVisibility(View.GONE);
					}else  if(flag==4){
						button.setOnClickListener(new OnClickListener() {
							@Override
							public void onClick(View arg0) {
								Intent intent = new Intent(context, ShippingStatusActivity.class);
								intent.putExtra("good_id", goods_list.get(index).goods_id);
								intent.putExtra("order_id",list.get(position).order_id);
								((Activity)context).startActivityForResult(intent, 0);
							}
						});
					}
				}else{
					button.setVisibility(View.GONE);
				}
			}
			
//			convertView.setTag(holder);
		/*} else {
			holder = (ViewHolder) convertView.getTag();
		}	*/
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Date currentTime = new Date(order.order_time);
		holder.time.setText(format.format(currentTime));
		holder.sno.setText(order.order_sn);
		holder.fee.setText(order.formated_shipping_fee);
		holder.total.setText(order.total_fee);
		
		if(flag == 1) {
			for(int i=0;i<goods_list.size();i++) {
				button.setVisibility(View.GONE);
			}
			holder.check.setVisibility(View.VISIBLE);
			holder.ok.setBackgroundResource(R.drawable.button_narrow_red);
			holder.ok.setText(resource.getString(R.string.pay));
			holder.check.setText(resource.getString(R.string.balance_cancel));
			holder.ok.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					 
					Message msg = new Message();
	                msg.what = 1;
                    msg.obj = order;
                    parentHandler.handleMessage(msg);
				}
			});
			
			holder.check.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					 
					Message msg = new Message();
	                msg.what = 2;
                    msg.obj = order;
                    parentHandler.handleMessage(msg);
				}
			});
			
		} else if(flag == 2) {
			for(int i=0;i<goods_list.size();i++) {
				button.setVisibility(View.GONE);
			}
			holder.ok.setVisibility(View.GONE);
			holder.check.setVisibility(View.GONE);
		} else if(flag == 3) {
			for(int i=0;i<goods_list.size();i++) {
				button.setVisibility(View.GONE);
			}
			holder.ok.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					 
					Message msg = new Message();
	                msg.what = 3;
                    msg.obj = order;
                    parentHandler.handleMessage(msg);
				}
			});
		} else if(flag == 4) {
			holder.ok.setVisibility(View.GONE);
			holder.check.setVisibility(View.GONE);
		}
		return convertView;
	}
	
	class ViewHolder {
		private TextView sno;
		private TextView time;
		private LinearLayout body;
		private TextView fee;
//		private TextView red_paper;
//		private TextView score;
		private TextView total;
		private Button check;
		private Button ok;
	}
}
